package com.cungmua.app.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.handler.PastDealHandler;
import com.cungmua.app.model.PastDeal;
import com.cungmua.app.utils.AppUtils;
import com.koushikdutta.urlimageviewhelper.UrlImageViewCallback;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kelvin.nguyen on 6/25/14.
 */
public class ListHisotryOrderAdapter extends BaseAdapter {
    private final Activity context;
    private final LayoutInflater inflater;
    private List<PastDeal> dataSource;
    private PastDealHandler handler;
    private int status = 0;
//    private AQuery aq;
    public ListHisotryOrderAdapter(Activity context, PastDealHandler handler) {
        this.context = context;
        this.handler = handler;
        this.inflater = LayoutInflater.from(context);
        this.dataSource = new ArrayList<PastDeal>();
//        aq=new AQuery(context);
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        status = dataSource.get(position).getStatus();
        if (status == 0)
            return 0;
        else
            return 1;
    }

    @Override
    public int getCount() {
        return dataSource.size();
    }

    @Override
    public PastDeal getItem(int i) {
        return dataSource.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public List<PastDeal> getDataSource() {
        return dataSource;
    }

    public void addToDataSource(List<PastDeal> newPage) {
        this.dataSource.addAll(newPage);
        notifyDataSetChanged();
    }
    public void setDataSource(List<PastDeal> dataSource) {
        this.dataSource = dataSource;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        TextView txtTitle, txtPrice, txtCreatedAt, txtStatus;
        ImageView imgImage;
        Button btnDestroy;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final PastDeal deal = dataSource.get(pos);

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_items_past_deals, parent, false);
            holder.imgImage = (ImageView) convertView.findViewById(R.id.imgImage);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
            holder.txtPrice = (TextView) convertView.findViewById(R.id.txtPrice);
            holder.txtCreatedAt = (TextView) convertView.findViewById(R.id.txtCreatedAt);
            holder.txtStatus = (TextView) convertView.findViewById(R.id.txtStatus);
            holder.btnDestroy = (Button) convertView.findViewById(R.id.btnDestroy);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //  UrlImageViewHelper.setUrlDrawable(holder.imgImage, deal.getImageUrl());


        // UrlImageViewHelper.setUrlDrawable(holder.imgImage, deal.getImageUrl());
        UrlImageViewHelper.setUrlDrawable(holder.imgImage, deal.getImageUrl(), new UrlImageViewCallback() {
            @Override
            public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {

                int width = loadedBitmap.getWidth();
                int height = loadedBitmap.getHeight();
                int screen_width = 232;
                Display display = context.getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                screen_width = size.x;

                if (height >= width) {

                    imageView.getLayoutParams().height = screen_width;
                    imageView.getLayoutParams().width = screen_width;
                } else {
                    imageView.getLayoutParams().height = 232;
                    imageView.getLayoutParams().width = screen_width;
                }

            }
        });

//        String imageUrl = deal.getImageUrl();
//        aq.id(holder.imgImage).image(imageUrl, true, true, 0, R.drawable.default_image);

        String priceString = context.getString(R.string.full_price_string, AppUtils.getFullPriceString(deal.getPrice()));
        holder.txtPrice.setText(priceString);
        SimpleDateFormat f = AppUtils.getShowDateFormatter();
        holder.txtCreatedAt.setText(f.format(deal.getCreatedAt()));
        holder.txtTitle.setText(deal.getShortName());
        holder.txtStatus.setText(deal.getStatusName());


        if (status == 1) {
            holder.btnDestroy.setVisibility(View.INVISIBLE);
            holder.txtStatus.setTextColor(Color.parseColor("#767676"));
        } else {
            holder.btnDestroy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    handler.onButtonDestroyClicked(deal);
                }
            });
        }

        return convertView;
    }


}
